
from flask import jsonify, request

from db.dao import StockLifelineDao
from db.models import StockLifeline
from service.requests.Eastmoney import Eastmoney
from utils.util import FlaskTool, CommonUtil
from web import custom
from datetime import datetime
'''
自选股->生命线
手动新增生命线
'''

@custom.route('/lifeline/comboBox-stocks', methods=['GET', 'POST'])
def queryAllStockByLines():
    '''
    股票下拉框(查询现有生命线里的股票)
    :return:
    '''
    stocks = StockLifelineDao().queryAllStocks()

    return jsonify(stocks)

@custom.route('/lifeline/add', methods=['POST'])
def addLifeline():
    '''
    手动新增生命线（只能选择生命线集合里已有的股票）
    :return:
    '''
    lifeline = StockLifeline()
    setattr(lifeline, 'line_code',CommonUtil.get_uuid())
    setattr(lifeline, 'stock_name', request.form['stock_name'])
    setattr(lifeline, 'stock_code', request.form['stock_code'])
    setattr(lifeline, 'line_type', 0)
    setattr(lifeline, 'line_mode', 0)
    setattr(lifeline, 'point_1_time', request.form['line_date'])
    setattr(lifeline, 'point_1_price', request.form['line_price'])
    # setattr(lifeline, 'drift', request.form[''])
    setattr(lifeline, 'create_time', datetime.now())

    StockLifelineDao().save(lifeline)

    return 'ok'


@custom.route('/lifeline/remove')
def remove_lifeline():
    '''
    手动删除生命线
    :return:
    '''
    lifeline_id = request.args.get('lifeline_id', type=int)
    if lifeline_id:
        StockLifelineDao().removeById(lifeline_id)
    return 'ok'

@custom.route('/lifeline/stock-price')
def query_cur_price():
    '''
    查询当前股价
    '''

    stock_code = request.args.get('stock_code', type=str)
    if stock_code:
        stockFundsHolder = Eastmoney.get_realtime_quotes(stock_code)
        reuslt_dict = {'stock_name': getattr(stockFundsHolder, 'stock_name'),
                       'stock_code': getattr(stockFundsHolder, 'stock_code'),
                       'open': getattr(stockFundsHolder, 'open'),
                       'now': getattr(stockFundsHolder, 'now'),
                       'high': getattr(stockFundsHolder, 'high'),
                       'low': getattr(stockFundsHolder, 'low')
                       }
        return jsonify(reuslt_dict)


@custom.route('/lifeline/stock-pressureline')
def query_perssureline():
    '''
    压力位
    '''

    stock_code = request.args.get('stock_code', type=str)
    if stock_code:
        stockFundsHolder = Eastmoney.get_realtime_quotes(stock_code)
        cur_price = None
        if stockFundsHolder:
            cur_price = getattr(stockFundsHolder, 'now')
            if (not cur_price) or ('-' == cur_price):
                cur_price = getattr(stockFundsHolder, 'preclose')
        lifelines = StockLifelineDao().queryUpperByPrice(stock_code, cur_price)

        return jsonify(lifelines)


@custom.route('/lifeline/stock-supportline')
def query_supportline():
    '''
    支撑线
    '''

    stock_code = request.args.get('stock_code', type=str)
    if stock_code:
        stockFundsHolder = Eastmoney.get_realtime_quotes(stock_code)
        cur_price = None
        if stockFundsHolder:
            cur_price = getattr(stockFundsHolder, 'now')
            if (not cur_price) or ('-' == cur_price):
                cur_price = getattr(stockFundsHolder, 'preclose')
        lifelines = StockLifelineDao().queryLowerByPrice(stock_code, cur_price)

        return jsonify(lifelines)


@custom.route('/lifeline/query-all-list')
def query_all_lines():
    '''
    支撑线
    '''

    pagination = StockLifelineDao().queryAllLinesByStock(request.args)

    return jsonify(FlaskTool.paginatioToList(pagination))
